<?php
/**
 *
 *
 * 功能说明：系统管理。
 *
 **/

namespace Qwadmin\Controller;

class SystemController extends ComController
{
    public function index()
    {
    	$glist = M('auth_group')->select();
    	$ulist = M('member')->select();
    	$list = M('people')->select();
    	
    	// dump($list);
    	// exit;
    	$time = date('Y');
    	$this->assign('ulist',$ulist);
    	$this->assign('glist',$glist);
    	$this->assign('list',$list);
    	$this->assign('time',$time);
    	$this->display();
    }



    public function eadd()
    {
    	$model=M("plan");
    	// dump(I());
    	// exit;
    	$plist['yue'] = substr($_POST['j_time'],5,2);
    	$plist['bu'] = $_POST['bu'];
    	// $plist['ment'] = $_POST['ment'];
    	$gg['yue'] = substr($_POST['j_time'],5,2);
    	$gg['gwei'] = $_POST['gwei'];

    	// echo $yue;
    	// exit;
    	
    	if (empty($_POST['gwei'])) {
    		$llist = $model->where($plist)->select();
    		// dump($llist);
    		// exit;
    		if (!empty($llist)) {

	    		echo "<script>alert('本月已导入".$plist['ment']."的绩效考核表,不要重复导入.');window.location.href='".__APP__."/System/index'</script>";
	    		exit;
	    	}
    	}else{
    		// dump($gg);
    		$glist = $model->where($gg)->select();
    		// dump($glist);
    		// exit;
    		if (!empty($glist)) {
	    		echo "<script>alert('本月已导入".$gg['gwei']."的绩效考核表,不要重复导入.');window.location.href='".__APP__."/System/index'</script>";
	    		exit;
	    	}
    	}
    	
    	
		if (! empty ( $_FILES ['file'] ['name']))
		{
			$tmp_file = $_FILES ['file'] ['tmp_name'];
			// echo $tmp_file;
			// exit;
			$file_types = explode (".",$_FILES ['file'] ['name'] );
			$file_type = $file_types[count( $file_types)-1];
			// if (strtolower( $file_type ) !== "xls" && strtolower( $file_type ) !== "xlsx" && strtolower( $file_type ) !== "csv"){
			// 	echo "<script>alert('不是Excel文件，重新上传');window.location.href='".__APP__."/StorageOut/list'</script>";
				
			// }
			$savePath = './Public/upfile/Excel';

			$str = date ( 'Ymdhis' );
			$file_name = $str . "." . $file_type;
			if (! copy ($tmp_file, $savePath.$file_name)){
				echo("failed");
			}else{
				$excelpath=$savePath . $file_name;
				$tableCols = array(
					'angle','target','norm','weight', 'value','rule','ment','ment','j_time','time','j_name','type'
				);
				$data=importExcel($excelpath);
				$ulist = M('member')->where(['uid'=>$_SESSION['uid']])->find();
				// dump($_POST);
				// dump($data);
				// exit;
				if (empty($_POST['gwei'])) {
					foreach ($data as $va) {
						// dump($va);
						
						$list['angle'] = $va[0];
						// $mtype = substr($list['angle'],0,6);
						$list['target'] = $va[1];
						$list['norm'] = $va[2];
						$list['weight'] = $va[3] * 100;
						if (is_numeric($va[4])) {
							$list['value'] = $va[4] * 100;
						}else{
							$list['value'] = $va[4] ;
						}
						
						$list['rule'] = $va[5];
						$list['num'] = $va[6];
						$list['bu'] = $_POST['bu'];
						$list['j_time'] = $_POST['j_time'];
						$list['time'] = date('Y-m-s');
						$list['j_name'] = $ulist['user'];
						$list['type'] = '绩效考核表';
						$list['yue'] =  substr($_POST['j_time'],5,2);
						switch (substr($va[0],0,6)) {
							case '财务':
									$list['m_type'] = 1;
								break;
							case '客户':
									$list['m_type'] = 2;
								break;
							case '内部':
									$list['m_type'] = 3;
								break;
							case '学习':
									$list['m_type'] = 4;
								break;
							
						}
						
						// $list['num'] ++;
						
						
						$arr[] = $list;
					}
					$ulist = M('member')->where(['ment'=>$_POST['bu']])->field('user,ment,uid')->select();
					// dump($ulist);
					// exit;
					// foreach ($ulist as  $va) {
					// 	$uu['uname'] = $va['user'];
					// 	$uu['ment'] = $va['ment'];
					// 	// $uu['post'] = $va['post'];
					// 	$uu['uid'] = $va['uid'];
					// 	$uu['yue'] = substr($_POST['j_time'],5,2);
					// 	$uu['time'] = date('Y-m-d');

					// 	$ae[] = $uu;
					// }
					// P($_POST['ment']);
					// exit;
					if (M('plan')->addALL($arr) > 0 ) {
						// $plist = M('plan')->order('id desc')->find();
						// dump($plist);
						// exit;.
						// if (M('people')->addALL($ae) > 0) {
							echo "<script>alert('导入成功');window.location.href='".__APP__."/System/jihua?bu=".$_POST['bu']."'</script>";
						// }
						
					}else{
						echo "<script>alert('导入失败');window.location.href='".__APP__."/System/index'</script>";
					}
				}else{
					foreach ($data as $va) {
						// dump($va);
						
						$list['angle'] = $va[0];
						// $mtype = substr($list['angle'],0,6);
						$list['target'] = $va[1];
						$list['norm'] = $va[2];
						$list['weight'] = $va[3] * 100;
						if (is_numeric($va[4])) {
							$list['value'] = $va[4] * 100;
						}else{
							$list['value'] = $va[4] ;
						}
						
						$list['rule'] = $va[5];
						$list['num'] = $va[6];
						// $list['bu'] = $_POST['bu'];
						$list['j_time'] = $_POST['j_time'];
						$list['gwei'] = $_POST['gwei'];
						$list['time'] = date('Y-m-s');
						$list['j_name'] = $ulist['user'];
						$list['type'] = '绩效考核表';
						$list['yue'] =  substr($_POST['j_time'],5,2);
						switch (substr($va[0],0,6)) {
							case '财务':
									$list['m_type'] = 1;
								break;
							case '客户':
									$list['m_type'] = 2;
								break;
							case '内部':
									$list['m_type'] = 3;
								break;
							case '学习':
									$list['m_type'] = 4;
								break;
							
						}
						
						// $list['num'] ++;
						
						
						$arr[] = $list;
					}


					$ulist = M('member')->where(['ment'=>$_POST['bu'],'post'=>$_POST['gwei']])->field('user,ment,post,uid')->select();
					// dump($ulist);
					// exit;
					// foreach ($ulist as  $va) {
					// 	$uu['uname'] = $va['user'];
					// 	$uu['ment'] = $va['ment'];
					// 	// $uu['post'] = $va['post'];
					// 	$uu['uid'] = $va['uid'];
					// 	$uu['yue'] = substr($_POST['j_time'],5,2);
					// 	$uu['time'] = date('Y-m-d');
					// 	$uu['btype'] = '绩效考核表';

					// 	$ae[] = $uu;
					// }

					if (M('plan')->addALL($arr) > 0 ) {
						// $plist = M('plan')->order('id desc')->find();
						// dump($plist);
						// exit;.
						// if (M('people')->addALL($ae) > 0) {
							// dump($ae);
							// exit;
							echo "<script>alert('导入成功');window.location.href='".__APP__."/System/jihua?gwei=".$_POST['gwei']."'</script>";
						// }
						
					}else{
						echo "<script>alert('导入失败');window.location.href='".__APP__."/System/index'</script>";
					}


				}
				
				// echo $mtype;
				// dump($arr);
				// exit;
				
				// dump($ulist);
				// exit;
				
				
				// dump($ae);
				// exit;
				// foreach ($ulist as  $v) {
				// 	foreach ($arr as $val) {
				// 		$v['']
				// 	}
				// }
				
					
					
			
				
			}
		}else{
			echo "<script>alert('请上传文件');window.location.href='".__APP__."/System/index'</script>";
			
		}
    }







    //选择评分人界面
    public function jihua()
    {

    	// dump(I());
    	// exit;

    	if (empty($_GET['bu'])) {
    		$plist['yue'] = date('m');
    		$plist['gwei'] = $_GET['gwei'];
    		$jlist = M('plan')->where($plist)->select();
    		
    	}else{
    		$plist['yue'] = date('m');
    		$plist['bu'] = $_GET['bu'];
    		$jlist = M('plan')->where($plist)->select();
    	}
    	// echo date('m');
    	
    	// foreach ($b as $v) {
     //      	$u_id .= "`id`="."'".$v['u_id']."'"." or ";
     //    }
     //    $u_id = rtrim($u_id,'or ');
    	
    	// dump($m);
    	// exit;
    	if (empty($jlist[0]['gwei'])) {
    		$gwei = 0;
    	}else{
    		$gwei = 1;
    	}
    	// echo $gwei;
    	// exit;
    	
		$m = M('member')->where(['user'=>['neq','admin']])->select();
    	// dump($mlist);
    	// exit;
    	$this->assign('jlist',$jlist);
    	$this->assign('gwei',$gwei);
    	$this->assign('mlist',$m);
    	$this->display();
    }





    //选定评分人
    public function xuan()
    {
    	// P(I());
    	// exit;
    	// $plist = M('ping')->where(['pid']);
    	$list = $_POST;
    	// $list['time'] = date('Y-m-s');
    	 // P($res);
      //   exit;
    	$users = array_pop($list);
    	// P($list);
    	// exit;
        $res   = [];
        foreach ($users as $user) {
        		$arr = $list;
        		$arr['time'] = date('Y-m-d');
        		$arr['pname'] = $user;
            	$res[]        = $arr;

            
        }
        // P($res);
        // exit;
        foreach ($res as $v) {
            // echo $v['n_id'];
            // exit;
            $pname .= $v['pname'].',';
        }
        $ment = $res[0]['ment']; 
        $yue = $res[0]['yue']; 
        $plist['pname'] = rtrim($pname,', ');
        $plist['pid'] = $res[0]['pid'];


        $dn['pname'] = rtrim($pname,', ');

       	$condition['ment'] = $ment;
		$condition['post'] = $ment;
		$condition['_logic'] = 'OR';
		// $y['yue'] = $res[0]['yue']; 
  		$ulist = M('member')->where($condition)->select();
  		// P($ulist);
  		// exit;
  		$arr = [];
  		foreach ($ulist as $v) {
  			$arr['uname'] = $v['user'];
  		}
  		// P($arr);
  		// exit;
      // echo $pname;
        // P($y);
        // exit;
        // $ae = arrToOne($ae);
		// print_r($ae);
		// P($res);
		
		$map['_complex'] = $condition;
		$map['yue']  = array('eq',$yue);
        $ckong = M('people')->where($map)->select();
        // P($ckong);
        // exit;
     
        if (empty($ckong)) {
        	//添加到评分人表
        	if (M('ping')->addALL($res) > 0) {
        		//添加到总和表
        		if (M('people')->where(['ment'=>$ment,'yue'=>$yue])->save($plist) > 0) {
        			//添加到绩效考核表
        			if (M('plan')->where(['id'=>$res[0]['pid']])->save($dn) > 0) {
			        	echo '1111';
			        }
        		}
        	}else{
        		echo '2222';
        	}
        }else{
        	if (M('ping')->addALL($res) > 0) {

			}else{
        		echo '2222';
        	}
        }

        // $char = json_encode($ae,true);
        // echo $char;
        // exit;
        // if (M('ping')->addALL($res) > 0) {
        // 	if (M('people')->where(['ment'=>$ment,'yue'=>$yue])->save($plist) > 0) {
        		
        // 	}
        	
        // }else{
        // 
        // 	echo '2222';
        // }
    	// ($list); 
    }

}